package com.channel.temperature.collect.ui.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.github.mikephil.charting.data.Entry
import com.kongzue.dialogx.dialogs.WaitDialog
import com.channel.temperature.collect.app.util.MyUtil
import com.channel.temperature.collect.data.response.LineDataBean
import com.channel.temperature.collect.data.response.OfflineBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.hgj.mvvmhelper.base.BaseViewModel
import java.util.Date


class OfflineDataModel : BaseViewModel() {
    var clickType = 0
    var mLiveLineData: MutableLiveData<LineDataBean> = MutableLiveData<LineDataBean>()
    var index = 0
    var values1: ArrayList<Entry> = ArrayList<Entry>()
    var values2: ArrayList<Entry> = ArrayList<Entry>()
    var values3: ArrayList<Entry> = ArrayList<Entry>()
    var values4: ArrayList<Entry> = ArrayList<Entry>()
    var values5: ArrayList<Entry> = ArrayList<Entry>()
    var values6: ArrayList<Entry> = ArrayList<Entry>()

    var leftMaxValue = 200.0f
    var rightMaxValue = 120.0f
    var leftMinValue = -25.0f
    var rightMinValue = -10f

    var xValueListData = ArrayList<String>()
    var isShareAction = false
    fun setLineChartData(listData: ArrayList<OfflineBean>) {

        viewModelScope.launch(Dispatchers.Main) {

            val result = withContext(Dispatchers.IO) {
                if (listData.size > 0) {
                    for (i in 0 until listData.size) {
                        var bean = listData.get(i)
                        var time = bean.time
                        var desString =
                            "时间：${
                                MyUtil.DateToStr(
                                    Date(
                                        (time.split("&").get(0).toLong()) * 1000
                                    )
                                )
                            }\n板载温度:${
                                MyUtil.formatFloat(bean.gX2, 2, 0.00f)
                            }℃" + "\n" + "环境温度:${
                                MyUtil.formatFloat(bean.pT2, 3, 0.000f)
                            }℃" + "\n" + "环境压力:${
                                MyUtil.formatFloat(bean.pressure2, 3, 0.000f)
                            }MPa"
//                        values1.add(
//                            Entry(
//                                i.toFloat(),
//                                gx1 / 100f,
//                                desString
//                            )
//                        )
                        values2.add(
                            Entry(
                                i.toFloat(),
                                bean.gX2,
                                desString
                            )
                        )

//                        values3.add(
//                            Entry(
//                                i.toFloat(),
//                                PT1Temperature1,
//                                desString
//                            )
//                        )
                        values4.add(
                            Entry(
                                i.toFloat(),
                                bean.pT2,
                                desString
                            )
                        )
//                        values5.add(
//                            Entry(
//                                i.toFloat(),
//                                pressure1Result.toFloat(),
//                                desString
//                            )
//                        )
                        values6.add(
                            Entry(
                                i.toFloat(),
                                bean.pressure2,
                                desString
                            )
                        )
                        xValueListData.add(
                            MyUtil.DateToStr(
                                Date(
                                    (time.split("&").get(0).toLong()) * 1000
                                )
                            )
                        )
                    }


                }

            }

            Log.e("测试", "测试55555")
            WaitDialog.dismiss(200)
            mLiveLineData.value = LineDataBean(
                values2 = values2,
                values4 = values4,
                values6 = values6,
            )
        }


    }

    /**
     * gxTemperature gx温度
     * voltage 基准电阻
     */
    fun getPT1000Temperature(gxTemperature: Float, voltage: Float): Float {
        var Rt = gxTemperature / voltage * 1000f
        var PT1000_A = 0.0039083f
        var PT1000_B = -0.0000005775f
        var PT1000_R = 1000f
        var temp: Float
        temp = PT1000_A * PT1000_A - 4 * PT1000_B * (PT1000_R - Rt) / PT1000_R
        temp = Math.sqrt(temp.toDouble()).toFloat()
        temp = temp - PT1000_A
        temp = temp / (2 * PT1000_B)
        return temp
    }


}


